
//
//  VideoOtherContentCell.m
//  ShowYork
//
//  Created by hxh on 2020/9/5.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "VideoOtherContentCell.h"

@interface VideoOtherContentCell ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField *contentTF;
@property (nonatomic,strong)UIView *lineView;
@property (nonatomic,assign)XHOtherReportType type;
@end

@implementation VideoOtherContentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    self.contentTF = [[UITextField alloc]init];
    self.contentTF.textColor = RGBA(51, 51, 51, 1);
    self.contentTF.font = font(14);
    self.contentTF.tintColor = RGBA(245, 75, 100, 1);
    self.contentTF.returnKeyType = UIReturnKeyDone;
    self.contentTF.delegate = self;
    [self.contentTF addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.contentView addSubview:self.contentTF];
    
    [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).offset(-10);
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
    }];
    
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = RGBA(248, 248, 248, 1);
    [self.contentView addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.left.equalTo(self.contentView);
        make.height.mas_equalTo(10);
    }];
    
    Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel = object_getIvar(self.contentTF, ivar);
    placeholderLabel.textColor = RGBA(204, 204, 204, 1);
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"VideoOtherContentCell";
    VideoOtherContentCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[VideoOtherContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)setOtherContent:(NSString *)content type:(XHOtherReportType)reportType isMine:(BOOL)isMine{
    if (reportType == XHOTHERRERORTPHONE) {
        self.contentTF.placeholder = @"手机号";
        self.contentTF.keyboardType = UIKeyboardTypeNumberPad;
    }
    if (reportType == XHOTHERRERORTQQ) {
        self.contentTF.placeholder = @"QQ号";
        self.contentTF.keyboardType = UIKeyboardTypeNumberPad;
    }
    if (reportType == XHOTHERRERORTMAIL) {
        self.contentTF.placeholder = @"邮箱";
        self.contentTF.keyboardType = UIKeyboardTypeDefault;
    }
    self.contentTF.text = content;
    self.type = reportType;
    
  
    
}
#pragma mark - <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    if ([self.delegate respondsToSelector:@selector(videoOtherContentEndEditing:type:)]) {
        [self.delegate videoOtherContentEndEditing:textField.text type:self.type];
    }
    return YES;
}

- (void)textDidChange:(UITextField *)textField{
    
      NSString *toBeString = textField.text;
   
      //获取高亮部分
      UITextRange *selectedRange = [textField markedTextRange];
      UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
   
      // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
      if (!position)
      {
          if (toBeString.length > 20)
          {
              NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:20];
              if (rangeIndex.length == 1)
              {
                  textField.text = [toBeString substringToIndex:20];
              }
              else
              {
                  NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 20)];
                  textField.text = [toBeString substringWithRange:rangeRange];
              }
          }
          
      }
}

@end
